<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="futuresFormRules" :model="futuresForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="futuresForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="futuresForm.title" />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-tag
          :key="index"
          v-for="(keyword,index) in futuresForm.keyword"
          closable
          :disable-transitions="false"
          style="margin-right:10px; margin-bottom:10px;"
          @close="handleKeywordRemove(index)">
          {{keyword}}
        </el-tag>
        <el-input
          v-if="keywordFormHidden == false"
          v-model="keywordForm.title"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleKeywordConfirm"
          @blur="handleKeywordConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="handleKeywordHidden">+新建</el-button>
      </el-form-item>

        <el-form-item label="上市日期">
          <el-date-picker 
            v-model="futuresForm.marketDate"
            align="right"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交易所">
          <el-select v-model="futuresForm.exchangeID" placeholder="请选择">
            <el-option
              v-for="item in exchangeOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID">
            </el-option>
          </el-select>          
        </el-form-item>
      <el-form-item label="图片" prop="cover">
        <Upload v-model="futuresForm.cover" />
      </el-form-item>
      <el-form-item label="最后交易日" prop="lastTradeDay">
        <el-input v-model="futuresForm.lastTradeDay" />
      </el-form-item>
      <el-form-item label="最后交割日" prop="lastDeliveryDay">
        <el-input v-model="futuresForm.lastDeliveryDay" />
      </el-form-item>
      <el-form-item label="最小变动价位" prop="minimum">
        <el-input v-model="futuresForm.minimum" />
      </el-form-item>
      <el-form-item label="交割单位" prop="deliveryUnit">
        <el-input v-model="futuresForm.deliveryUnit" />
      </el-form-item>
      <el-form-item label="合约月份" prop="contract">
        <el-input v-model="futuresForm.contract" />
      </el-form-item>
      <el-form-item label="介绍" prop="description">
        <el-input v-model="futuresForm.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="介绍" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="futuresForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="futuresForm.status.value === 5" type="danger" @click="handleDelete(futuresForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveFutures, removeFuturesById, getFutures } from '@/api/knowledge/futures'
import { getExchangeAvabile } from '@/api/knowledge/exchange'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      pickerOptions: {
          disabledDate(time) {
            //return time.getTime() < Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      exchangeOptions:[],
      futuresForm: {
        code: '',
        title: '',
        remark: '',
        keyword:[],
        status: {
          label: '',
          value: 1
        }
      },
      futuresFormRules: [],
      keywordFormHidden:true,
      keywordForm:{
        title:""
      }
    }
  },
  created() {
    this.loadExchangeList()
    let futuresID = this.$route.query && this.$route.query.futuresID
    if (futuresID !== undefined) {
      futuresID = parseInt(futuresID)
      if (!isNaN(futuresID)) {
        this.listLoading = true
        const formData = {
          'futuresID': futuresID
        }
        getFutures(formData).then(response => {
          if (response.status === 200) {
            this.futuresForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(futuresID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'futures' })
    },
    handleKeywordConfirm:function(){
      let keyword = this.keywordForm.title;
      if (keyword) {
        this.futuresForm.keyword.push(keyword);
      }
      this.keywordFormHidden = true;
      this.keywordForm.title = '';
    },
    handleKeywordHidden:function(){
      this.keywordFormHidden = false
      this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleKeywordRemove:function(index){
      this.futuresForm.keyword.splice(index, 1);
    },
    loadExchangeList(){
      
      getExchangeAvabile().then(response => {
        this.exchangeOptions = response.data.list
      })
    },
    createData() {
      this.listLoading = true
      const futuresForm = this.futuresForm
      futuresForm.status = futuresForm.status.value
      console.log(futuresForm)
      if (futuresForm.ID > 0) {
        futuresForm.futuresID = futuresForm.ID
      }
      futuresForm.keyword = futuresForm.keyword.join('>>')
      saveFutures(futuresForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'futures' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'futuresID': row.ID,
      }
      removeFuturesById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'futures' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
